#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <mysql/mysql.h>
#include "./base_CGI.h"
#include "./decode.h"


int main()
{
  //获取参数
  char buf[1024 * 4] = {0};
  fprintf(stderr, "获取失败\n");
  if(Get_info(buf) < 0)
  {
    fprintf(stderr, "Get_info failed!\n");
    return 1;
  }
  fprintf(stderr, "Get_info OK\n");
  fprintf(stderr, "Get_info OK:%s\n",buf);
  urldecode(buf);
  fprintf(stderr, "Get_info OK:%s\n",buf);

  char name[1024] = {0};
  char * p = buf;
  p += strlen("name=");
  int i = 0;
  while(*p != '&' && *p != '\0')
  {
    name[i++] = *p;
    p++;
  }
  name[i++] = '\0';

  fprintf(stderr,"name :%s\n",name);




//将构造的html文件写给父进程
 int fd = open("./table.html",O_RDONLY,666);
 if(fd < 0)
 {
   printf("\nopen failed fd %d\n",fd);
   return 0;
 }
 char c = '\0';
 int read_size = read(fd , &c, 1);
 fprintf(stderr, "read_size:%d\n",read_size);

  while(read_size)
  {
    read_size = read(fd, &c,1);
    fprintf(stderr,"read_size %d\n",read_size);
    fprintf(stderr,"##################\n");
    fprintf( stderr,"%c",c);
    printf("%c",c);
  }
  close(fd);

  MYSQL * connect_fd = mysql_init(NULL);
  if(mysql_real_connect(connect_fd, "127.0.0.1", "root", "1", "ForOffer", 3306, NULL, 0) == NULL)
  {
    fprintf(stderr, "mysql_real_connect failed!\n");
    return 1;
  }
  //处理字符集
  fprintf(stderr,"\n-----chow_info.c-------%d-----%s-----\n",__LINE__,__FUNCTION__);
  if ( mysql_set_character_set(connect_fd, "utf8"  )  ) 
  { 
    fprintf ( stderr , "错误, %s\n" , mysql_error( connect_fd)  ) ; 
  } 



  

}

